/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.editor.gui.view.internal;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.URL;
import java.rmi.server.UID;

import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import com.genia.toolbox.basics.editor.AbstractController;
import com.genia.toolbox.basics.editor.gui.AbstractGUI;
import com.genia.toolbox.basics.editor.gui.AbstractToolBar;
import com.genia.toolbox.basics.editor.gui.view.internal.panel.ElementViewPanel;
import com.genia.toolbox.basics.editor.model.document.Document;
import com.genia.toolbox.basics.editor.model.document.Element;

/**
 * The view internal frame.
 * 
 * @param <G>
 *          The generic gui.
 * @param <C>
 *          The generic controller.
 * @param <D>
 *          The generic document.
 * @param <E>
 *          The generic element.
 * @param <F>
 *          The generic view factory.
 */
@SuppressWarnings("serial")
public abstract class AbstractDocumentInternalFrame<G extends AbstractGUI<?, ?, C, D, E, ?>, C extends AbstractController<?, G, D, E, ?>, D extends Document<E>, E extends Element<?>, F extends AbstractViewFactory<G, ?, D, E>>
    extends JInternalFrame
    implements InternalFrameListener
{

  /**
   * The application gui.
   */
  public G gui;

  /**
   * The document model.
   */
  private D document;

  /**
   * The selected element.
   */
  private E selectedElement = null;

  /**
   * The root view panel.
   */
  private ElementViewPanel<G, ?, E, ?> viewPanel = null;

  /**
   * The view factory.
   */
  private F elementViewFactory = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          the application gui.
   * @param model
   *          the document model.
   */
  public AbstractDocumentInternalFrame(G gui, D model)
  {
    super("", true, true, true, true);
    this.gui = gui;
    this.document = model;
    this.elementViewFactory = this.createViewFactory();
    this.setSize(800, 600);
    this.addInternalFrameListener(this);

    URL url = AbstractToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/icon.gif");
    this.setFrameIcon(new ImageIcon(url));

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    this.getContentPane().setLayout(layout);

    this.viewPanel = this.createPanel(this.getDocumentWithoutUpdate().getElementModel());
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5, 5, 5, 5);
    JScrollPane scrollpane = new JScrollPane((JPanel) this.viewPanel);
    this.getContentPane().add(scrollpane, constraints);
  }



  /**
   * Get the view factory.
   * 
   * @return the view factory.
   */
  public abstract F createViewFactory();



  /**
   * Get the view panel.
   * 
   * @return the view panel.
   */
  public ElementViewPanel<G, ?, E, ?> getViewPanel()
  {
    return this.viewPanel;
  }



  /**
   * Create the view panel from an element.
   * 
   * @param model
   *          the element model.
   * @return the view panel.
   */
  public ElementViewPanel<G, ?, E, ?> createPanel(E model)
  {
    return this.elementViewFactory.createView(model);
  }



  /**
   * Set the view panel.
   * 
   * @param viewPanel
   *          the view panel.
   */
  public void qetViewPanel(ElementViewPanel<G, ?, E, ?> viewPanel)
  {
    this.viewPanel = viewPanel;
  }



  /**
   * Get the application gui.
   * 
   * @return the application gui.
   */
  public G getGui()
  {
    return this.gui;
  }



  /**
   * Handle the activation internal frame events.
   * 
   * @param e
   *          the internal frame event.
   */
  public void internalFrameActivated(InternalFrameEvent e)
  {
    this.getGui().displayDocument(this.document);
  }



  /**
   * Handle the opened internal frame events.
   * 
   * @param e
   *          the internal frame event.
   */
  public void internalFrameOpened(InternalFrameEvent e)
  {
    // Do nothing
  }



  /**
   * Handle the closed internal frame events.
   * 
   * @param e
   *          the internal frame event.
   */
  public void internalFrameClosed(InternalFrameEvent e)
  {
    this.setSelectedElement(null);
    this.getGui().displayElementSettings(null);
  }



  /**
   * Handle the closing internal frame events.
   * 
   * @param e
   *          the internal frame event.
   */
  public void internalFrameClosing(InternalFrameEvent e)
  {
    this.setSelectedElement(null);
    this.getGui().getController().notifyCloseDocument(this.getDocumentWithoutUpdate());
  }



  /**
   * Handle the deactivated internal frame events.
   * 
   * @param e
   *          the internal frame event.
   */
  public void internalFrameDeactivated(InternalFrameEvent e)
  {
    this.setSelectedElement(null);
    this.getViewPanel().deactivateAll();
  }



  /**
   * Handle the deiconified internal frame events.
   * 
   * @param e
   *          the internal frame event.
   */
  public void internalFrameDeiconified(InternalFrameEvent e)
  {
    this.setSelectedElement(null);
    this.getGui().displayDocument(this.getDocumentWithoutUpdate());
  }



  /**
   * Handle the iconified internal frame events.
   * 
   * @param e
   *          the internal frame event.
   */
  public void internalFrameIconified(InternalFrameEvent e)
  {
    this.setSelectedElement(null);
    this.getGui().displayElementSettings(null);
  }



  /**
   * Deactivate the content of the document.
   */
  public void deactivateAll()
  {
    this.getViewPanel().deactivateAll();
  }



  /**
   * Get the displayed document model.
   * 
   * @return the document model.
   */
  public D getDocument()
  {
    this.getDocumentWithoutUpdate().setElementModel(this.getViewPanel().getElement());
    return this.getDocumentWithoutUpdate();
  }



  /**
   * Set the displayed document model.
   * 
   * @param document
   *          the document.
   */
  public void setDocumentModel(D document)
  {
    this.setDocumentWithoutUpdate(document);
    this.updateTitle();
  }



  /**
   * Update the internal frame title.
   */
  public abstract void updateTitle();



  /**
   * Get the displayed document model.
   * 
   * @return the document model.
   */
  protected D getDocumentWithoutUpdate()
  {
    return this.document;
  }



  /**
   * Get the displayed document model.
   * 
   * @param document
   *          the document model.
   */
  protected void setDocumentWithoutUpdate(D document)
  {
    this.document = document;
  }



  /**
   * Get the selected element.
   * 
   * @return the selected element.
   */
  public E getSelectedElement()
  {
    return this.selectedElement;
  }



  /**
   * Set the selected element.
   * 
   * @param selectedElement
   *          the selected element.
   */
  public void setSelectedElement(E selectedElement)
  {
    this.selectedElement = selectedElement;
    this.getGui().refresh();
  }



  /**
   * Set the displayed element model.
   * 
   * @param elementModel
   *          the porlet.
   */
  public void setElementModel(E elementModel)
  {
    this.getViewPanel().updateElement(elementModel);
    this.updateTitle();
  }



  /**
   * Get the view panel from the specified unique ID of an element.
   * 
   * @param elementUniqueID
   *          the element unique ID.
   * @return the view panel.
   */
  public ElementViewPanel<G, ?, E, ?> getViewPanel(UID elementUniqueID)
  {
    return this.getViewPanel().getViewPanel(elementUniqueID);
  }



  /**
   * Add a sub element to a container.
   * 
   * @param elementModel
   *          the container.
   */
  public void addSubElement(E elementModel)
  {
    ElementViewPanel<G, ?, E, ?> view = this.getViewPanel(elementModel.getUniqueID());
    if (view != null) {
      view.addSubElement();
    }
  }



  /**
   * Delete a sub element.
   * 
   * @param elementModel
   *          the element.
   */
  public void deleteSubElement(E elementModel)
  {
    ElementViewPanel<G, ?, E, ?> view = this.getViewPanel(elementModel.getUniqueID());
    if (view != null) {
      view.deleteSubElement();
    }
  }



  /**
   * Move up or right a sub element in a container.
   * 
   * @param elementModel
   *          the element.
   */
  public void moveUpOrRightSubElement(E elementModel)
  {
    ElementViewPanel<G, ?, E, ?> view = this.getViewPanel(elementModel.getUniqueID());
    if (view != null) {
      view.moveUpOrRight();
    }
  }



  /**
   * Move down or left a sub element in a container.
   * 
   * @param elementModel
   *          the element.
   */
  public void moveDownOrLeftSubElement(E elementModel)
  {
    ElementViewPanel<G, ?, E, ?> view = this.getViewPanel(elementModel.getUniqueID());
    if (view != null) {
      view.moveDownOrLeft();
    }
  }



  /**
   * Update the saving status of the document.
   */
  public void notifyDocumentChanges()
  {
    this.getDocumentWithoutUpdate().setIsSaved(false);
    this.updateTitle();
  }



  /**
   * Get the view factory.
   * 
   * @return the view factory.
   */
  public F getViewFactory()
  {
    return this.elementViewFactory;
  }

}
